package shop;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Create By IntelliJ IDEA
 *
 * @Author: jsonor
 * @Create-Time: 2017/10/10 11:20
 * @Description: 添加商品到购物车
 */
@WebServlet(name = "add2Car", urlPatterns = {"/add2Car" })
public class Add2CarServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException {

    Map<String, String> map;

    String thingId = request.getParameter("id");
    HttpSession session = request.getSession();

    Object car = session.getAttribute("car");
    if (car == null) {
       map = new HashMap<String, String>();
    } else {
      map = (Map<String, String>)car;
    }

    // map中就有用户所有的购物车中的信息
    map.put(thingId, thingId);
    session.setAttribute("car", map);

    response.sendRedirect("/shoppingCar");
  }
}
